package com.playball.lottery.frm.yhzx;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.playball.lottery.R;
import com.playball.lottery.ApplicationData;
import com.playball.lottery.base.BaseActivity;
import com.playball.lottery.customview.ShowTipDialog;
import com.playball.lottery.nt.GetRequestParams;


/**
 * 安全中心 - 手机绑定
 * 
 * @author tanglizhong
 * @version 2014-3-26 上午10:37
 */
public class ScTelephoneActivity extends BaseActivity {

	/** 标题栏 */
	private TextView info;

	/**
	 * 未绑定手机布局
	 */
	private View view_sc_telephone_unbind;
	/** 手机号码 */
	private EditText yhzx_sc_et_telephone;
	/** 验证码 */
	private EditText yhzx_sc_et_verification_code;
	/** 获取验证码按钮 */
	private Button yhzx_sc_btn_verification_code;
	/** 立即绑定按钮 */
	private Button yhzx_sc_btn_bind;

	/**
	 * 已绑定手机布局
	 */
	private View view_sc_telephone_binded;
	/** 手机号码 */
	private TextView yhzx_sc_tv_telephone_number;
	/** 更换手机按钮 */
	private Button yhzx_sc_btn_change_bind;

	/**
	 * 重新绑定手机布局
	 */
	private View view_sc_telephone_rebind;
	/** 原手机号码 */
	private EditText yhzx_sc_et_telephone_old;
	/** 新手机号码 */
	private EditText yhzx_sc_et_telephone_new;
	/** 验证码 */
	private EditText yhzx_sc_et_verification_code_new;
	/** 获取验证码 */
	private Button yhzx_sc_btn_verification_code_new;
	/** 立即绑定按钮 */
	private Button yhzx_sc_btn_rebind;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.yhzx_sc_telephone);

		initView();
		titleBar();
		initData();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
	}

	public void onPause() {
		super.onPause();
		
	}

	/**
	 * 初始化数据
	 */
	private void initData() {
		yhzx_sc_tv_telephone_number.setText(ApplicationData.getInstance().getMobile());

		if (ApplicationData.getInstance().isIsMobileValied()) {
			view_sc_telephone_unbind.setVisibility(View.GONE);
			view_sc_telephone_binded.setVisibility(View.VISIBLE);
			view_sc_telephone_rebind.setVisibility(View.GONE);
		} else {
			view_sc_telephone_unbind.setVisibility(View.VISIBLE);
			view_sc_telephone_binded.setVisibility(View.GONE);
			view_sc_telephone_rebind.setVisibility(View.GONE);
		}
	}

	/**
	 * 初始化组件
	 */
	private void initView() {
		info = (TextView) findViewById(R.id.top_title_info);

		view_sc_telephone_unbind = findViewById(R.id.view_sc_telephone_unbind);
		yhzx_sc_et_telephone = (EditText) findViewById(R.id.yhzx_sc_et_telephone);
		yhzx_sc_et_verification_code = (EditText) findViewById(R.id.yhzx_sc_et_verification_code);
		yhzx_sc_btn_verification_code = (Button) findViewById(R.id.yhzx_sc_btn_verification_code);
		yhzx_sc_btn_verification_code.setOnClickListener(btn_onclick);
		yhzx_sc_btn_bind = (Button) findViewById(R.id.yhzx_sc_btn_bind);
		yhzx_sc_btn_bind.setOnClickListener(btn_onclick);

		view_sc_telephone_binded = findViewById(R.id.view_sc_telephone_binded);
		yhzx_sc_tv_telephone_number = (TextView) findViewById(R.id.yhzx_sc_tv_telephone_number);
		yhzx_sc_btn_change_bind = (Button) findViewById(R.id.yhzx_sc_btn_change_bind);
		yhzx_sc_btn_change_bind.setOnClickListener(btn_onclick);

		view_sc_telephone_rebind = findViewById(R.id.view_sc_telephone_rebind);
		yhzx_sc_et_telephone_old = (EditText) findViewById(R.id.yhzx_sc_et_telephone_old);
		yhzx_sc_et_telephone_new = (EditText) findViewById(R.id.yhzx_sc_et_telephone_new);
		yhzx_sc_et_verification_code_new = (EditText) findViewById(R.id.yhzx_sc_et_verification_code_new);
		yhzx_sc_btn_verification_code_new = (Button) findViewById(R.id.yhzx_sc_btn_verification_code_new);
		yhzx_sc_btn_verification_code_new.setOnClickListener(btn_onclick);
		yhzx_sc_btn_rebind = (Button) findViewById(R.id.yhzx_sc_btn_rebind);
		yhzx_sc_btn_rebind.setOnClickListener(btn_onclick);
	}

	/**
	 * 按钮点击事件
	 */
	View.OnClickListener btn_onclick = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.yhzx_sc_btn_verification_code:
				telephone_new = yhzx_sc_et_telephone.getText().toString();
				telephone = "";
				if (telephone_new.length() == 11) {
					AsyncLoader_Verification verification = new AsyncLoader_Verification();
					verification.execute("103");
				} else {
					yhzx_sc_et_telephone.setError("请输入正确的手机号码");
				}
				break;
			case R.id.yhzx_sc_btn_bind:
				info.setText(R.string.yhzx_sc_telephone);
				telephone = yhzx_sc_et_telephone.getText().toString();
				telephone_new = telephone;
				verification_code = yhzx_sc_et_verification_code.getText().toString();
				if (telephone.length() != 11) {
					yhzx_sc_et_telephone.setError("请输入正确的手机号码");
				} else if (verification_code.length() != 6) {
					yhzx_sc_et_verification_code.setError("请输入6位验证码");
				} else {
					AsyncLoader_Bind bind = new AsyncLoader_Bind();
					bind.execute("104");
				}
				break;
			case R.id.yhzx_sc_btn_change_bind:
				info.setText(R.string.yhzx_sc_telephone_change);
				telephone = "";
				telephone_new = "";
				verification_code = "";
				yhzx_sc_et_telephone_old.setText("");
				yhzx_sc_et_telephone_new.setText("");
				yhzx_sc_et_verification_code_new.setText("");
				view_sc_telephone_unbind.setVisibility(View.GONE);
				view_sc_telephone_binded.setVisibility(View.GONE);
				view_sc_telephone_rebind.setVisibility(View.VISIBLE);
				break;
			case R.id.yhzx_sc_btn_verification_code_new:
				telephone = yhzx_sc_et_telephone_old.getText().toString();
				telephone_new = yhzx_sc_et_telephone_new.getText().toString();
				if (telephone.length() == 11) {
					if (telephone_new.length() == 11) {
						AsyncLoader_Verification verification = new AsyncLoader_Verification();
						verification.execute("103");
					} else {
						yhzx_sc_et_telephone_new.setError("请输入正确的手机号码");
					}
				} else {
					yhzx_sc_et_telephone_old.setError("请输入正确的手机号码");
				}
				break;
			case R.id.yhzx_sc_btn_rebind:
				info.setText(R.string.yhzx_sc_telephone);
				telephone = yhzx_sc_et_telephone_old.getText().toString();
				telephone_new = yhzx_sc_et_telephone_new.getText().toString();
				verification_code = yhzx_sc_et_verification_code_new.getText().toString();
				if (telephone.length() != 11) {
					yhzx_sc_et_telephone_old.setError("请输入正确的手机号码");
				} else if (telephone_new.length() != 11) {
					yhzx_sc_et_telephone_new.setError("请输入正确的手机号码");
				} else if (verification_code.length() != 6) {
					yhzx_sc_et_verification_code_new.setError("请输入6位验证码");
				} else {
					AsyncLoader_Bind bind = new AsyncLoader_Bind();
					bind.execute("104");
				}
				break;
			}
		}
	};

	/** TitleBar标题栏 */
	public void titleBar() {
		info.setText(R.string.yhzx_sc_telephone);
		TextView back = (TextView) findViewById(R.id.top_title_back);
		back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	/** 获取验证码的手机号 */
	private String telephone = "";
	private String telephone_new = "";
	private String verification_code = "";

	/**
	 * 获取手机验证码
	 */
	private class AsyncLoader_Verification extends AsyncTask<String, Integer, String> {
		ShowTipDialog showdialog = new ShowTipDialog();

		@Override
		protected void onPreExecute() {
			showdialog.showDialog(ScTelephoneActivity.this);
		}

		@Override
		protected String doInBackground(String... params) {
			String action = params[0];
			String strResult;

			try {
				JSONObject param = new JSONObject();
				param.put("Mobile", telephone_new);
				param.put("OldMobile", telephone);

				String UserID = ApplicationData.getInstance().getUserID();
				strResult = GetRequestParams.getRequestUserIDResult(param.toString(), UserID, action,
						getApplicationContext());
				return strResult;
			} catch (JSONException e) {
				// e.printStackTrace();
				return null;
			}
		}

		@Override
		protected void onPostExecute(String result) {
			try {
				if (null != result) {
					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					if (js.getInt("Code") == 0) {

					}
					String data = js.getString("Data");
					showTip(data);
				}
			} catch (JSONException e) {
				// e.printStackTrace();
			} finally {
				showdialog.closeDialog();
			}
		}

	}

	/**
	 * 绑定手机
	 */
	private class AsyncLoader_Bind extends AsyncTask<String, Integer, String> {
		ShowTipDialog showdialog = new ShowTipDialog();

		@Override
		protected void onPreExecute() {
			showdialog.showDialog(ScTelephoneActivity.this);
		}

		@Override
		protected String doInBackground(String... params) {
			String action = params[0];
			String strResult;

			try {
				JSONObject param = new JSONObject();
				// param.put("Mobile", telephone_new);
				param.put("PhoneCode", verification_code);

				String UserID = ApplicationData.getInstance().getUserID();
				strResult = GetRequestParams.getRequestUserIDResult(param.toString(), UserID, action,
						getApplicationContext());
				return strResult;
			} catch (JSONException e) {
				// e.printStackTrace();
				return null;
			}
		}

		@Override
		protected void onPostExecute(String result) {
			try {
				if (null != result) {
					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					if (js.getInt("Code") == 0) {
						// 绑定成功
						ApplicationData.getInstance().setMobile(
								telephone_new.substring(0, 3) + "***"
										+ telephone_new.substring(telephone_new.length() - 3));
						ApplicationData.getInstance().setIsMobileValied(true);

						yhzx_sc_tv_telephone_number.setText(ApplicationData.getInstance().getMobile());

						view_sc_telephone_unbind.setVisibility(View.GONE);
						view_sc_telephone_binded.setVisibility(View.VISIBLE);
						view_sc_telephone_rebind.setVisibility(View.GONE);
					}
					String data = js.getString("Data");
					showTip(data);
				}
			} catch (JSONException e) {
				// e.printStackTrace();
			} finally {
				showdialog.closeDialog();
			}
		}

	}

	/** 显示文字提示 */
	private void showTip(String tip) {
		Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
	}

}
